package edu.androidecs.condescendingquiz;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

public class QuizManager
{
	Context cntxt;
	public Quiz quizList[];
	
	public static final int QUIZNUM=10;
	
	public ArrayList<QuestionLoadObject> loadObjects;
	
	public static final String QUIZFILE="questions.xml";
	
	public int curQuizNum=0;
	
	public QuizManager(Context activityContext)
	{
		int i;
		cntxt=activityContext;
		quizList=new Quiz[QUIZNUM];
		
		loadObjects=new ArrayList<QuestionLoadObject>();
		
		for(i=0;i<QUIZNUM;i++)
		{
			quizList[i]=new Quiz();
		}
		
		mapLevels();
	}
	
	public Quiz fetchExplicitLevel(int levelNum)
	{
		return quizList[levelNum];
	}
	
	public Quiz fetchNextLevel()
	{
		curQuizNum++;
		
		if(curQuizNum>=QUIZNUM)
		{
			return null;
		}
		
		return quizList[curQuizNum-1];
	}
	
	//Maps all questions based on level.
	public void mapLevels()
	{
		//ArrayList<QuestionLoadObject> arrayListList=new ArrayList[QUIZNUM];
		int i;
		
		for(i=0;i<loadObjects.size();i++)
		{
			quizList[loadObjects.get(i).questionLevel].addQuestion(loadObjects.get(i));
		}
		
	}
	
	/**
	 * This function loads and parses an XML file representing every level. This will probably work..
	 * It might not. It was written by Michael, so whine at him if it's broken.
	 */
	public void loadAllData()
	{
		AssetManager assetLocator=cntxt.getAssets();
		int i, j;
		
		Log.i("CondescendingQuiz", "Attempting to start loading from XML...");
		
		try
		{
			DocumentBuilder docBuild=DocumentBuilderFactory.newInstance().newDocumentBuilder();
			InputStream assetStream=assetLocator.open(QUIZFILE);
			Document doc=docBuild.parse(assetStream);
			
			Log.i("CondescendingQuiz", "I was able to load the questions in XML!");
			
			//This should mean that there's a question tag here.
			NodeList inputNodeList=doc.getDocumentElement().getChildNodes();
			
			Log.i("CondescendingQuiz", "InputNodeList length: "+inputNodeList.getLength());
			
			for(i=0;i<inputNodeList.getLength();i++)
			{
				Node n=inputNodeList.item(i);
				
				Log.i("CondescendingQuiz", "Finding data for node #"+i+", data="+n.getTextContent());
				
				if(n instanceof Element)
				{
					QuestionLoadObject qlo=new QuestionLoadObject();
					NodeList questionNodes=n.getChildNodes();
					Log.i("CondescendingQuiz", "Current node size="+questionNodes.getLength());
					
					for(j=0;j<questionNodes.getLength();j++)
					{
						Node nodeMeister=questionNodes.item(j);
						
						if(nodeMeister instanceof Element)
						{
							
							String content=nodeMeister.getLastChild().getTextContent().trim();
							Log.i("CondescendingQuiz", "Current node name="+nodeMeister.getNodeName());
							
							if(nodeMeister.getNodeName().equalsIgnoreCase("level"))
							{
								qlo.questionLevel=Integer.parseInt(content)-1;
							}
							
							else if(nodeMeister.getNodeName().equalsIgnoreCase("category"))
							{
								qlo.questionCategory=content;
							}
							
							else if(nodeMeister.getNodeName().equalsIgnoreCase("qtext"))
							{
								qlo.questionText=content;
							}
							
							else if(nodeMeister.getNodeName().equalsIgnoreCase("ans1"))
							{
								qlo.answerChoice1=content;
							}
							
							else if(nodeMeister.getNodeName().equalsIgnoreCase("ans2"))
							{
								qlo.answerChoice2=content;
							}
							
							else if(nodeMeister.getNodeName().equalsIgnoreCase("ans3"))
							{
								qlo.answerChoice3=content;
							}
							
							else if(nodeMeister.getNodeName().equalsIgnoreCase("ans4"))
							{
								qlo.answerChoice4=content;
							}
							
							else if(nodeMeister.getNodeName().equalsIgnoreCase("correct"))
							{
								qlo.correctAnswerChoice=Integer.parseInt(content)-1;
							}
							
							else if(nodeMeister.getNodeName().equalsIgnoreCase("insult"))
							{
								qlo.insultText=content;
							}
						}
						
						
					}
					
					loadObjects.add(qlo);
					
					Log.i("CondescendingQuiz", "Current QuestionLoadObject="+qlo);
				}
			}
			
			mapLevels();
			
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			Log.e("CondescendingQuiz", "ERR: Couldn't fetch quiz file.");
		} catch (ParserConfigurationException e)
		{
			// TODO Auto-generated catch block
			Log.e("CondescendingQuiz", "ERR: Parser configuration exception, evidently.");
		} catch (SAXException e)
		{
			// TODO Auto-generated catch block
			Log.e("CondescendingQuiz", "ERR: Couldn't parse the file, buddy!");
			e.printStackTrace();
		}
	}
}
